package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionServer;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @desc 权限管理类
 * @Date 2020/10/26 14:18
 */

@RestController
@RequestMapping("/permission")
public class PermissionController {


    @Reference
    private PermissionServer permissionServer;


    /**
     * 查询所有的权限
     *
     * @param queryPageBean 查询需要的条件 页码 条数 条件
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        //获取到了页码，条数和查询条件，调用业务层进行操作
        PageResult pageResult = permissionServer.findPage(queryPageBean);

        return pageResult;
    }

    /**
     * 添加权限
     *
     * @param permission 添加的内容
     * @return
     */
    @PreAuthorize("hasRole('ROLE_ADMIN')")   //必须系统管理员才能操作
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {

        try {
            //获取到了页码，条数和查询条件，调用业务层进行操作
             permissionServer.add(permission);

            return new Result(true, MessageConstant.ADD_PERMISSIN_SUCCESS);

        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false, MessageConstant.PERMISSIONS_EXIST);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSIN_FALL);
        }


    }



    /**
     * 查询所有的权限
     *
     * @param id 查询需要的id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {

        try {
            //通过id查询，返回权限的详细的信息
            Permission permission = permissionServer.findById(id);

            return new Result(true, MessageConstant.QUERY_PERMISSIN_SUCCESS,permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSIN_FALL);
        }

    }


    /**
     * 修改权限
     * @param permission 修改后的值
     * @return
     */
    @PreAuthorize("hasRole('ROLE_ADMIN')")  //必须管理员才能操作
    @RequestMapping("/update")
    public Result update(@RequestBody Permission permission) {

        try {
            //通过前端的数据查询数据库
           permissionServer.update(permission);

            return new Result(true, MessageConstant.EDIT_PERMISSIN_SUCCESS);
        } catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false, MessageConstant.PERMISSIONS_EXIST);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSIN_FALL);
        }

    }


    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            //通过前端的数据查询数据库
            permissionServer.delete(id);

            return new Result(true, MessageConstant.DELETE_PERMISSIN_SUCCESS);
        } catch (RuntimeException e){

            e.printStackTrace();
            return new Result(false, MessageConstant.PERMISSIONS_USE);

        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSIN_FALL);
        }
    }


    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/findAll.do")
    public Result findAll() {
        try {
            List<Permission> list = permissionServer.findAll();
            return new Result(true, MessageConstant.QUERY_PERMISSIN_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSIN_FALL);
        }
    }



}
